package com.skyline.wxjumphack;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static com.skyline.wxjumphack.Hack.ADB_PATH;

public class KaLun {


    static String ADB = ADB_PATH + " shell input ";
    static String BACK = "keyevent 4";
    static String TAP = "tap ";
    // 250 250 300 300
    static String SWIPE = "swipe ";


    public static void main(String... strings) {

        String root = KaLun.class.getResource("/").getPath();
        System.out.println("root: " + root);
        File srcDir = new File(root, "imgs/kaluninput");
        srcDir.mkdirs();
        System.out.println("srcDir: " + srcDir.getAbsolutePath());

        MyPosFinder.setrgb(255, 0, 0);
//        MyPosFinder.setrgb(255, 62, 62);

        MyPosFinder myPosFinder = new MyPosFinder();

//        while (true) {
//
//
//
//        }
        // 自动消除红点
        long nowtime = System.currentTimeMillis() / 1000;
        while (true) {

            File file = new File(srcDir, System.currentTimeMillis() / 1000 - nowtime + ".png");
            if (file.exists()) {
                file.deleteOnExit();
            }
            try {
                Thread.sleep(1000);
                //截图不是实时的,大概在一秒前的
                Process process = Runtime.getRuntime().exec(ADB_PATH + " shell /system/bin/screencap -p /sdcard/screenshot_123.png");

                process.waitFor();
                process = Runtime.getRuntime().exec(ADB_PATH + " pull /sdcard/screenshot_123.png " + file.getAbsolutePath());
                process.waitFor();

                System.out.println("screenshot, file: " + file.getAbsolutePath());
                BufferedImage image = ImgLoader.load(file.getAbsolutePath());
                System.out.printf("imagew" + image.getWidth() + "imageh" + image.getHeight());

                int w = image.getWidth();
                int h = image.getHeight();


                int[] pos = myPosFinder.kalunfind(image);

                // 取不到红点再见
                if (pos == null) {
                    System.out.println("找不到红点退出");
                    return;
                }

                // 可能需要翻页
                if (pos[1] > h - h / 4) {
                    swippe(new int[]{w / 2, h / 2, w / 2, 100});
                    continue;
                }

                tap(pos);
                // 切换界面需要反应计算时间
                Thread.sleep(1000);

                exec(BACK);


            } catch (Exception e) {
                e.printStackTrace();
                return;
            }

        }
    }

    private static void swippe(int[] pos) {
        exec(SWIPE + pos[0] + " " + pos[1] + " " + pos[2] + " " + pos[3] + " 500");
        // 休眠时间和滚动时间相同
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void tap(int[] pos) {
        exec(TAP + pos[0] + " " + pos[1]);
    }

    public static void exec(String strings) {
        System.out.println(strings + System.currentTimeMillis() / 1000);


        try {
            Runtime.getRuntime().exec(ADB + strings);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
